<?php
namespace app\interfaces\controller;

use app\interfaces\model\MemberMessage as MemberMessageModel;
use app\interfaces\model\Member as MemberModel;
use app\interfaces\model\MemMess as MemMessModel;
use think\Request;
use think\Exception;
use think\View;

class MemberMessage
{

    /*
     * 用户端 - 消息中心
     * @param  string $user_id 会员ID
     */
    public function message_list()
    {

        // 获取参数
        $user_id = Request::instance()->param('user_id');

        if (empty($user_id)) throw new Exception('not Empty');

        $model = new MemberMessageModel();
        //$create_time = (new MemberModel())->where('id',$user_id)->value('create_time');
        $day_time = date('Y-m-d H:i:s',time());

        // 验证数据
        //$str = $user_id.','.'0';
        //$condition['mem_mess.member_id'] = [['eq',''],['like','%'.$user_id.'%'],'or'];
        //$condition['create_time'] = ['elt',$day_time];
        $condition['mem_mess.member_id'] = ['eq',$user_id];
        $result = MemMessModel::alias('mem_mess')
            ->join('member','mem_mess.member_id = member.id')
            ->join('member_message','mem_mess.message_id = member_message.id')
            ->field('mem_mess.click,mem_mess.member_id,mem_mess.message_id id,member_message.title,member_message.content,member.create_time mm_create_time,mem_mess.member_str,member_message.create_time')
            ->where($condition)->fetchSql(false)->order('member_message.create_time', 'desc')->paginate(10)->each(function($item,$key){
                //$item['create_time'] = date('Y-m-d',$item['create_time']);
            });

//        foreach ($result as &$value) {
//            if(!$value['member_str']){
//                $value['click'] = 0;
//            }else{
//
//                (strpos($value['member_str'],$user_id) !== false) ?  $value['click'] = 1 : $value['click'] = 0;
//            }
//
//            $value['content'] = strip_tags($value['content']);
//            $value['create_time'] = date('Y-m-d',strtotime($value['create_time']));
//        }

        return ['code' => 200, 'message' => '成功', 'data' => $result];

    }

    public function clear_all(){
        $user_id = Request::instance()->param('user_id');
        $ret = db('mem_mess')->where('member_id',$user_id)->delete();
        if($ret !== false){
            return ['code' => 200, 'message' => '成功',];
        }
        return ['code' => 400, 'message' => '失败',];
    }
    public function detail()
    {
        $message_id = Request::instance()->param('message_id');
        $user_id = Request::instance()->param('user_id');

        if (empty($message_id)) throw new Exception('not Empty');

        $model = new MemMessModel();
        $ret = $model->where('message_id',$message_id)->find();
        if(empty($ret['member_str'])){
            $member_str = ','.$user_id.',';
        }else{
            $sql_member_str = $model->where('message_id',$message_id)->where('member_str','like',','.$user_id.',')->value('member_str');
            if(empty($sql_member_str)){
                $member_str = $ret['member_str'];
                $member_str .= $user_id.',';
            }
        }


//        if($ret){
//            $member_str .= $ret['member_str'].$user_id.',';
//        }else{
//            $member_str = ','.$user_id.',';
//        }

        $result = $model->save(['click' => 1],['message_id' => $message_id]);
        $result = db('member_message')->where('id',$message_id)->value('content');

        $view = new View();

        echo $view->fetch('', ['data' => $result]);

    }

    /*
     * 用户端 - 消息详情
     *
     */
    public function detail2()
    {
        $message_id = Request::instance()->param('message_id');
        $user_id = Request::instance()->param('user_id');

        if (empty($message_id)) throw new Exception('not Empty');

        $model = new MemMessModel();
        $ret = $model->where('message_id',$message_id)->find();
        if(empty($ret['member_str'])){
            $member_str = ','.$user_id.',';
        }else{
            $sql_member_str = $model->where('message_id',$message_id)->where('member_str','like',','.$user_id.',')->value('member_str');
            if(empty($sql_member_str)){
                $member_str = $ret['member_str'];
                $member_str .= $user_id.',';
            }
        }


//        if($ret){
//            $member_str .= $ret['member_str'].$user_id.',';
//        }else{
//            $member_str = ','.$user_id.',';
//        }

        $result = $model->save(['member_str' => $member_str],['message_id' => $message_id]);
        $result = db('member_message')->where('id',$message_id)->value('content');

        $view = new View();

        echo $view->fetch('', ['data' => $result]);

    }

    public function details()
    {
        $message_id = Request::instance()->param('message_id');
        $user_id = Request::instance()->param('user_id');

        if (empty($message_id)) throw new Exception('not Empty');

        $model = new MemberMessageModel();
        $member_str = '';
        $member_str_to_array = explode(',',$member_str);
        halt($member_str_to_array);
        if(!in_array($user_id,$member_str_to_array)){
            $member_str_to_array[] = $user_id;
        }

        $result = $model->save(['member_str' => implode(',',$member_str_to_array)],['id' => $message_id]);
        $result = $model->where('id',$message_id)->value('content');

        $view = new View();

        echo $view->fetch('', ['data' => $result]);

    }

    /*
      * 用户端 - 商家入驻
      *
      */
    public function merchants()
    {
        $view = new View();

        echo $view->fetch('');
    }


    public function merchants2()
    {
        $view = new View();

        echo $view->fetch('');
    }

}
